package com.example.demo.excel;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

import lombok.extern.slf4j.Slf4j;

/**
 * @author jiangjinwu
 * @version 1.0
 * @since 2025/10/1 21:58
 */

@Slf4j

public class StringConverter implements Converter<String>{
  @Override
  public Class<String> supportJavaTypeKey() {
    return String.class;
  }

  @Override
  public CellDataTypeEnum supportExcelTypeKey() {
    return CellDataTypeEnum.STRING;
  }

  /**
   * 读取 Excel 内容时调用
   */
  @Override
  public String convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
      GlobalConfiguration globalConfiguration) {
    // 直接返回单元格的原始字符串值
    return cellData.getStringValue();
  }

  /**
   * 写入 Excel 时调用
   */
  @Override
  public WriteCellData<?> convertToExcelData(String value, ExcelContentProperty contentProperty,
      GlobalConfiguration globalConfiguration) {
    return new WriteCellData<>(value);
  }
}
